﻿namespace ImageProcessing.UI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// IMG_BandFilter form
    /// </summary>
    public partial class IMG_BandFilter : Form
    {
        /// <summary>
        /// original image bitmap
        /// </summary>
        private Entities.IMG_Image imgOriginal;

        /// <summary>
        /// Edited image bitmap
        /// </summary>
        private Entities.IMG_Image imgEdited;

        /// <summary>
        /// parent Form referance
        /// </summary>
        private UI.IMG_HomeScreen parentForm;

        /// <summary>
        /// Initializes a new instance of the <see cref="IMG_BandFilter"/> class.
        /// </summary>
        /// <param name="img_original">The img_original.</param>
        /// <param name="parent">The parent.</param>
        public IMG_BandFilter(Entities.IMG_Image img_original, UI.IMG_HomeScreen parent)
        {
            try
            {
                this.InitializeComponent();
                this.parentForm = parent;
                if (img_original != null)
                {
                    this.imgOriginal = img_original;
                    this.picBoxOriginal.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(img_original);
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Loading Form");
            }
        }

        /// <summary>
        /// Handles the Click event of the btnOpen control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.ppm)|*.jpg; *.jpeg; *.gif; *.bmp; *.ppm";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string img_OpenedFilePath = openFileDialog1.FileName;
                    this.imgOriginal = IMG_Processor.IMG_OpenSave.IMG_Open(img_OpenedFilePath);
                    this.picBoxOriginal.Image = this.imgOriginal.BitmapOfImage();
                    this.imgEdited = null;
                    this.picBoxEdited.Image = null;
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, Can't Open Image");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_BandPass control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_BandPass_Click(object sender, EventArgs e)
        {
            try
            {
                this.imgEdited = ImageProcessing.ImageProcessor.IMG_BandFilter.IMG_BandPass((this.imgEdited == null ? this.imgOriginal : this.imgEdited), double.Parse(this.txtD0.Text), double.Parse(this.txtwidth.Text));
                this.picBoxEdited.Image = this.imgEdited.BitmapOfImage();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Band pass Filter");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_BandReject control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_BandReject_Click(object sender, EventArgs e)
        {
            try
            {
                this.imgEdited = ImageProcessing.ImageProcessor.IMG_BandFilter.IMG_BandReject((this.imgEdited == null ? this.imgOriginal : this.imgEdited), double.Parse(this.txtD0.Text), double.Parse(this.txtwidth.Text));
                this.picBoxEdited.Image = this.imgEdited.BitmapOfImage();
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Band reject Filter");
            }
        }

        /// <summary>
        /// Handles the FormClosing event of the IMG_BandFilter control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void IMG_BandFilter_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                this.parentForm.ResetImage(this.imgEdited == null ? this.imgOriginal : this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, closing");
            }
        }
    }
}
